package group

import (
	"api/common"
	"api/pb/group"
	"context"
	"errors"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteGroupLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteGroupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteGroupLogic {
	return &DeleteGroupLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// DeleteGroup 解散群聊
func (l *DeleteGroupLogic) DeleteGroup(req *types.GroupInfo) (resp *types.Empty, err error) {
	// 1. 校验参数
	if req.ID <= 0 {
		return nil, errors.New("群聊ID不能为空")
	}
	// 2. 获取当前用户ID
	userID, err := common.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}
	// 3. 修改群聊状态为解散
	_, err = l.svcCtx.GroupRPC.UpdateGroup(l.ctx, &group.GroupInfo{
		Id:     req.ID,
		UserId: userID,
		Status: common.GroupStatusDelete,
	})
	// 4. 删除群聊成员
	if err != nil {
		return nil, err
	}
	return &types.Empty{}, nil
}
